<%@ page contentType="text/html; charset=utf-8" language="java"  %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%@include file="/static/common/header.jsp"%>
<%@include file="/static/common/easyui.jsp"%>
<%@include file="/static/common/common.jsp"%>
<script src="${ctx}/static/jquery/jquery.form.js" type="text/javascript"></script>
<link href="${ctx}/static/css/main.css" rel="stylesheet">
<script>

	function getSelectedId(){
		var row = $('#orgTreeGridData').datagrid('getSelected');
		if (row){
			return row.id;
		}else{
			return "";
		}
	}
	function getSelectedRow(){
		var row = $('#orgTreeGridData').datagrid('getSelected');
		return row;
	}

	function modify(OPT){
		 mId=getSelectedRow();
		if(OPT=='N'){
		 	mId="";
		 }else if(mId==""){
			showInfoCenter("请选择要修改的数据！");
			return;
		 }else{
		 	mId=mId.orgCode
		 }
	 	openWin('机构维护','${ctx}/Mng/org/toAddOrg?id='+mId+'&stmp='+(new Date()).getTime());
	 }
	 
	function refreshOrgData(id){
		mId=getSelectedId();
		if(mId!=""){
			var pnode= $('#orgTreeGridData').treegrid('getParent', mId);
			if(pnode) mId=pnode.id;else mId="";
		}
		
		if(mId==""){
			$('#orgTreeGridData').treegrid('reload');
		}else{
			$('#orgTreeGridData').treegrid('reload', pnode.id);
		}
		$('#orgTreeGridData').treegrid('unselectAll');
	}

	var flag="";
	function toggleLay(opt){
		if(flag==opt)return;
		flag=opt;
		if(opt=='show'){
			$('#orgLayOut').layout('expand','east'); 
		}else{
			$('#orgLayOut').layout('collapse','east'); 
		}
	}

	function showOrgData(row){
		toggleLay('show');
		currentOrgRow=row;
		initUserGrid('${ctx}/Mng/user/org/'+row.orgCode);
		$('#userTable').datagrid({
		title:'当前部门：<font color=blue>'+row.text
		});
		$("#selectOrgId").val(row.id);
		showOrgAgentData(row);
	}
	function showOrgAgentData(row){
		currentOrgRow=row;
		$('#orgAgentDataGrid').datagrid({
		title:'当前部门： <font color=blue>'+row.text,
		iconCls:'icon-bumen',
		method:'post',
		nowrap: false,
		striped: true,
		collapsible:false,
		url: '${ctx}/air/airAgent/orgAgent/ORG/'+row.id,
		idField:'id',
		columns:[[
			{field:'ck',checkbox:true,width:2}, 
			{field:'officeCode',title:'office号',width:120},
			{field:'officeIATACode',title:'IATACode" />',width:120},
			{field:'name_zh',title:'中文名称',width:250},
			{field:'name_en',title:'英文名称',width:250},
			{field:'agentType',title:'代理类型',width:250,hidden:true},
			{field:'ttlSys',title:'系统',width:120,hidden:true},
			{field:'id',title:'系统编号',width:150,hidden:true}
		]],
		pagination:true,
		pageSize:20,
		rownumbers:true,
		onLoadSuccess:function(){
			$('#orgAgentDataGrid').datagrid('clearSelections'); //一定要加上这一句，要不然datagrid会记住之前的选择状态，删除时会出问题
		}
		});
	}
	
	function showOrgAgentSelectDataGrid(){
		$('#orgAgentSelectDataGrid').datagrid({
		title:'<font color=blue>代理人',
		iconCls:'icon-bumen',
		method:'post',
		nowrap: false,
		striped: true,
		collapsible:false,
		url: '${ctx}/air/airAgent/orgAgent/VIEW/'+$("#selectOrgId").val()+'?v='+(new Date()).getTime(),
		idField:'id',
		columns:[[
			{field:'ck',checkbox:true,width:2}, 
			{field:'officeCode',title:'office号',width:120},
			{field:'officeIATACode',title:'IATACode',width:120},
			{field:'name_zh',title:'中文名称',width:250},
			{field:'name_en',title:'英文名称',width:250},
			{field:'agentType',title:'代理类型',width:250,hidden:true},
			{field:'tempVar',title:'tempVar',width:120},
			{field:'id',title:'系统编号',width:150,hidden:true}
		]],
		checkOnSelect:false,
		pagination:true,
		pageSize:20,
		rownumbers:true,
		onLoadSuccess:function(data){
			$('#orgAgentDataGrid').datagrid('clearSelections'); //一定要加上这一句，要不然datagrid会记住之前的选择状态，删除时会出问题
			initDiableCheckbox($(this),data.rows); 
			 
		}
		});
	}
	
	
	/**
	 * 初始化置灰的选项
	 */
	function initDiableCheckbox(grid,rows){
			//$('#orgAgentSelectDataGrid input:checkbox').each(function(index,el){ if(rows[index]['tempVar']=='0')el.disabled=true; });
		for(var j=0;j<rows.length;j++){
			var state = rows[j]['tempVar'];
			if(state=="1"){
			  $(".datagrid-row[datagrid-row-index="+j+"] input[type='checkbox']")[0].disabled=true;
			}
		}
	}
	function saveSelectAgent(){
		var rows = $('#orgAgentSelectDataGrid').datagrid('getSelections');
		var s ='';
		for(var i=0; i<rows.length; i++){
			if (s != '') s += ',';
			s += rows[i]['id'];
		}
		if(s=='')return;
		$.ajax({
			type:"post",
			cache: false,
			dataType:"json",
			async:false,
			url: "${ctx}/air/airAgent/saveRelOrgAgent?stmp="+getStmp(),
			data: "orgId="+$("#selectOrgId").val()+"&agentIds="+s,
			beforeSend:function(){ 
				$("body").mask("正在处理中...");
			},
			success:function(jsonStr){
				$("body").unmask();
				if(jsonStr.success=="true"){
					showInfo();
				}else{
					showInfo("操作失败:"+jsonStr.err_msg);
				}
				clearSelect();
				$('#orgAgentSelectWin').window('close');
			}
		});
	}
	function clearSelect(){
		$('#orgAgentSelectDataGrid').datagrid('reload');
		$('#orgAgentDataGrid').datagrid('reload');
		$('#orgAgentDataGrid').datagrid('clearSelections');
		$('#orgAgentSelectDataGrid').datagrid('clearSelections');
	}
	
	function removeSelectAgent(){
		var rows = $('#orgAgentDataGrid').datagrid('getSelections');
		var s ='';
		for(var i=0; i<rows.length; i++){
			if (s != '') s += ',';
			s += rows[i]['id'];
		}
		$.ajax({
			type:"post",
			cache: false,
			dataType:"json",
			async:false,
			url: "${ctx}/air/airAgent/removeRelOrgAgent?stmp="+getStmp(),
			data: "orgId="+$("#selectOrgId").val()+"&agentId="+s,
			beforeSend:function(){ 
				$("body").mask("正在处理中...");
			},
			success:function(jsonStr){
				$("body").unmask();
				if(jsonStr.success=="true"){
					showInfo();
				}else{
					showInfo("操作失败:"+jsonStr.err_msg);
				}
				clearSelect();
			}
		});
	}
	
	function modifyAgent(opt){
		openWin3('orgAgentSelectWin','orgAgentSelectWin');
		showOrgAgentSelectDataGrid();
	}
</script>
</head>

<body id="orgLayOut" class="easyui-layout">
<div data-options="region:'center',collapsed:false,border:false" class="bmargin">
  <table id="orgTreeGridData" title="组织机构" class="easyui-treegrid"  
			data-options="
				url: '${ctx}/Mng/org/treeGridData',
				rownumbers: true,
				idField: 'id',
				treeField: 'orgCode',fit:true,animate:true,toolbar:'#orgToolbar',onDblClickRow:function(row){showOrgData(row);}
			">
    <thead>
      <tr>
        <th data-options="field:'orgCode'" width="120" align="left">orgCode</th>
		<th data-options="field:'orgType'<shiro:lacksPermission name="____AgentAdmin">,hidden:true</shiro:lacksPermission>" width="150" align="center">orgType</th>
        <th data-options="field:'nameCn'" width="150" >Name</th>
        <th data-options="field:'nameEn'" width="150">nameEn</th>
		<th data-options="field:'airCode'" width="150">airCode</th>
      </tr>
    </thead>
  </table>
</div>
<div data-options="region:'east',split:false,border:false,collapsed:true" style="width:560px;">
<!-- tab -->
<div class="easyui-tabs"  data-options="fit:true,border:false,plain:true">
	<div title="用户信息">
	 	<table id="userTable" title=""  data-options="singleSelect:true,fit:true"> </table>
	</div>
	
	<div title="代理人信息">
	 	<table id="orgAgentDataGrid" title=""  data-options="singleSelect:true,fit:true,toolbar:'#orgAgentToolbar'"> </table>
	</div>
</div>
<!-- end tab-->
</div>

<div id="orgToolbar" style="padding:5px;height:auto">
	  <div style="margin-bottom:5px">
	  <shiro:hasPermission name="____AgentAdmin">
		<a href="javascript:void(-1);" class="easyui-linkbutton" iconCls="icon-add" onClick="modify('N')" plain="true">新增</a> 
		<a href="javascript:void(-1);" class="easyui-linkbutton" iconCls="icon-edit" onClick="modify('E')" plain="true">修改</a> 
		<a href="javascript:void(-1);" class="easyui-linkbutton" iconCls="icon-reload" onClick="refreshOrgData()" plain="true">刷新</a> 
  		</shiro:hasPermission>
	</div>
</div>

<div id="orgAgentToolbar" style="padding:5px;height:auto">
	  <div style="margin-bottom:5px">
		<a href="javascript:void(-1);" class="easyui-linkbutton" iconCls="icon-add" onClick="modifyAgent('A')" plain="true">新增代理人</a> 
		<a href="javascript:void(-1);" class="easyui-linkbutton" iconCls="icon-remove" onClick="removeSelectAgent('D')" plain="true">移除代理人</a>
	</div>
</div>

</div>
<div id="orgAgentSelectWin" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-edit',title:'角色维护	'" style="width:580px;height:320px;padding:10px;">
	<form id="orgAgentSelectForm" method="post">
    <input type="hidden" id="selectAgentIds" name="agentIds"/>
	<input type="hidden" id="selectOrgId" name="orgId">
	</form>
	<table id="orgAgentSelectDataGrid" title=""  data-options="singleSelect:false,fit:true,toolbar:'#orgAgentSelectToolbar'"> </table>
	<div id="orgAgentSelectToolbar" style="padding:5px;height:auto">
		  <div style="margin-bottom:5px">
			<a href="javascript:void(-1);" class="easyui-linkbutton" iconCls="icon-save" onClick="saveSelectAgent()" plain="true">保存选取</a>
		</div>
	</div>
</div>
<div id="innerFramWindow" class="easyui-window" data-options="closed:true,modal:true,collapsible:false,minimizable:false,maximizable:false,draggable:true,onBeforeClose:function(){}" style="overflow: hidden">
  <iframe id="innerWinFram" scrolling="yes" frameborder="0"  src='' style="width:100%;height:100%;"></iframe>
</div>
<div id="userToolBar" class="datagrid-toolbar" style="padding:5px;height:auto">
            <a href="javascript:void(0)" onClick="addOrgUserRow();" id="btnadd" class="easyui-linkbutton" iconCls="icon-add" plain="true">新增</a>
			<shiro:hasPermission name="____AgentAdmin">
            <a href="javascript:void(0)" onClick="deleteUserRow();" id="btnremove" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
			</shiro:hasPermission>
            <a href="javascript:void(0)" onClick="updateUserRow(null);" id="btnedit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">更新</a>
            <a href="javascript:void(0)" onClick="restPass();" id="btnedit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">重置密码</a>                  
</div>
<%@include file="/WEB-INF/views/frm/account/userAdd.jsp"%>
</body>
</html>
